﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage_Admin extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		$this->output->set_header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		
		$this->allowed =array('admin', 'panelist');
		
		#load all database and required models
		$this->load->database();
		$this->load->model('common_model');
		$this->load->model('admin_model');
		$this->data = array();
		$this->data['admin_value'] = $this->session->all_userdata();
		if($this->data['admin_value']['admin_type'] == 'Web Admin' || $this->data['admin_value']['admin_type'] == 'Sub Admin')
		{
			$this->admin = 'admin';
		}
		else {
			$this->admin = 'panelist';
		}
	}
	
	public function createAdmin(){
		if($this->session->userdata('admin_type') == 'Web Admin') {
			if(isset($_POST['fullname'])){
				$this->load->library('form_validation');
				$this->form_validation->set_rules('fullname','First Name','required|xss_clean');
				$this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[scopus_admin.admin_email]|xss_clean');
				$this->form_validation->set_rules('password','Password','required|trim');
				$this->form_validation->set_rules('confirm_password','Confirm Password','required|trim|matches[password]');
				$this->form_validation->set_rules('contact_no','Contact Number','xss_clean');
				$this->form_validation->set_rules('Remark','Remark','xss_clean');
				$this->form_validation->set_message('matches',"Password not matched");
					if($this->form_validation->run()){
						
						if($this->admin_model->create_admin()){
							$config = Array(
							'protocol' => 'smtp',
							'smtp_host' => 'smtpout.asia.secureserver.net',
							'smtp_port' => 80,
							'smtp_user' => 'info.elsevier@logiquebraincreations.com',
							'smtp_pass' => 'default@123',
							'mailtype'  => 'html'
							);
							$this->load->library('email', $config);
							$this->email->set_newline("\r\n");
							$this->email->from('no-reply@elsevier.com', 'Elsevier');
							$this->email->to($this->input->post('email')); 
							$this->email->subject('[Scopus] Admin account');
							$this->load->library('email',array('mailtype'=>'html'));
							$message="<p>Hi ".ucwords($this->input->post('fullname')).", <br>You have been added as an Admin of Elsevier Award System.<br> Use the below details to login to your account: <br><strong>Email:</strong> '".$this->input->post('email')."'<br><strong>Password:</strong> '".$this->input->post('password')."' <br><br>Regards,<br>Elsevier Support</p>";
							$message.="<p>Check Scopus<a href='".BASE_URL."'>Click here</a> to Visit</p>";
							$this->email->message($message);
							$this->email->send();
							$this->session->set_userdata('message','Admin created sucessfully');
							header('Location: '.BASE_URL.'/'.$this->admin.'/create_admin');
							redirect(BASE_URL.'/'.$this->admin.'/create_admin');
							exit();
						}
						else {
							$this->session->set_userdata('message','There was some error. Please submit the form again');
							$this->load->view('admin/manage_admin/create_admin');
						}
					}
					else {
						$this->load->view('admin/manage_admin/create_admin');
					}
			}
			
			else{
				$this->load->view('admin/manage_admin/create_admin');
			}
			
		}
		else { 
				$this->session->sess_destroy();
				header('Location: '.BASE_URL.'/'.'login');
					redirect(BASE_URL.'/'.'/login');
					exit();			
		}
	}
	
	public function manageadmin(){
		if($this->session->userdata('admin_type') == 'Web Admin') {
			
			if(isset($_POST['submit'])){
				$this->form_validation->set_rules('name_search','Name','regex_match[/^[A-Za-z\. ]+$/]|xss_clean');
				$this->form_validation->set_rules('email_search','Email','trim|xss_clean');
				if($this->input->post('name_search')== '' && $this->input->post('email_search')==''){
					$this->session->set_userdata('err_search','Please enter name or email for search!');
					header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
					redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
					exit();
				}
				if($this->form_validation->run()) {
					$data['query']=$this->admin_model->search_admin();
					$this->load->view('admin/manage_admin/manage_admin',$data);
				}
				else {
					$this->load->model('admin_model');
					$data['query']=$this->admin_model->manage_admin();
					$this->load->view('admin/manage_admin/manage_admin',$data);
				}
			}
			else {
				$data["query"] = $this->admin_model->fetch_admin();
				$this->load->view('admin/manage_admin/manage_admin',$data);
			}
		}
		else { 
			header('Location: '.BASE_URL.'/'.'login');
			redirect(BASE_URL.'/'.'/login');
			exit();			
		}
	}
	
	public function editAdmin(){
		if($this->session->userdata('admin_type') == 'Web Admin') {
			if($this->input->get('id')){
				$id = htmlspecialchars(strip_tags($this->input->get('id')),ENT_QUOTES);	
			}
			else {
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
			$this->load->model('admin_model');
			$data['query']=$this->admin_model->manage_admin($id);
			if(isset($_POST['submit'])){
				$this->load->library('form_validation');
				$this->form_validation->set_rules('fullname','First Name','required|regex_match[/^[A-Za-z\. ]+$/]|xss_clean');
				$this->form_validation->set_rules('email','Email','required|trim|valid_email|callback_check_duplicate|xss_clean');
				$this->form_validation->set_rules('password','Password','trim|regex_match_ps[/^(?=^.{8,}$)(?=.*\d)(?=.*[!@#$%^&*]+)(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/]|min_length[6]|max_length[12]|');
				$this->form_validation->set_rules('confirm_password','Confirm Password','trim|min_length[6]|max_length[12]|matches[password]');
				$this->form_validation->set_rules('contact_no','Contact no','regex_match[/^[0-9 \-)(.\,\+]+$/]|xss_clean');
				$this->form_validation->set_rules('Remark','Remark','regex_match[/^[A-Za-z0-9\.\,\-\_\&\)(\ ]+$/]|xss_clean');
				$this->form_validation->set_message('is_unique',"This username is already exist");
				$this->form_validation->set_message('matches',"Password not matched");
				
				
				$this->form_validation->set_message('check_duplicate',"Email id already exist for another admin");
			}	
			if($this->form_validation->run()) {
				$this->load->model('admin_model');
				if($this->admin_model->update_admin($id)){
					$this->session->set_userdata('message','Updated Successfully');
					header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
					redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
					exit();
				}
			}
			
			$this->load->view('admin/manage_admin/edit_admin',$data);
		}
		else { 
				header('Location: '.BASE_URL.'/'.'login');
				redirect(BASE_URL.'/'.'/login');
				exit();			
		}
	}
	
	public function check_duplicate(){
		if($this->admin_model->check_duplicate()){
			return false;
		}
		else {
			return true;
		}
	}
	
	public function delete_admin(){
		if($this->session->userdata('admin_type') == 'Web Admin') {
			if($this->input->get('id')){
				$id = htmlspecialchars(strip_tags($this->input->get('id')),ENT_QUOTES);	
			}
			else {
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
			
			if($this->admin_model->delete_admin($id)){
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
		}
		else { 
			header('Location: '.BASE_URL.'/'.'login');
			redirect(BASE_URL.'/'.'/login');
			exit();			
		}
	}
	
	public function active_inactive(){
		if($this->session->userdata('admin_type') == 'Web Admin') {
			if($this->input->get('id')){
				$id = htmlspecialchars(strip_tags($this->input->get('id')),ENT_QUOTES);	
			}
			else {
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
			$status = $this->input->get('status');
			if($this->admin_model->active_inactive($id,$status)){
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
		}
		else { 
			header('Location: '.BASE_URL.'/'.'login');
			redirect(BASE_URL.'/'.'/login');
			exit();			
		}
	}
	
	public function update_admin(){
		if(isset($_POST['fullname'])){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('fullname','First Name','required|regex_match[/^[A-Za-z\. ]+$/]|xss_clean');
			$this->form_validation->set_rules('password','Password','callback_admin_password_match');
			$this->form_validation->set_rules('contact_no','Contact no','regex_match[/^[0-9 \-)(.\,\+]+$/]|xss_clean');
			
			if($this->form_validation->run()) {
				$this->admin_model->update_admin_details();
				$this->session->set_userdata('update_message','Update Sucessfully');
				header('Location: '.BASE_URL.'/'.$this->admin.'/update_profile');
				redirect(BASE_URL.'/'.$this->admin.'/update_profile');
				exit();
			}
		}
		$data['query']=$this->admin_model->get_info();
		$this->load->view('admin/manage_admin/admin_update_profile',$data);
	}
	
	public function admin_password_match(){
		if($_POST['password']!=' ' || $_POST['confirm_password']!=' '){
			if($_POST['password'] == $_POST['confirm_password']){
				$a=count($_POST['password']);
				if($a<6){
					$this->form_validation->set_message('admin_password_match',"Password not matched");
					return false;
				}else {
					$this->form_validation->set_message('admin_password_match',"Password must be 6 character");
					return true;
				}
				
			}
			else {
				$this->form_validation->set_message('admin_password_match',"Password not matched");
				return false;
			}
		}else {
			return true;
		}
	}
	
	public function createPanelist($value='')
	{
		if($this->session->userdata('admin_id') && $this->session->userdata('admin_id') != '' && $this->session->userdata('logedin'))
		{
			if($this->input->post('fullname'))
			{
			// Set validation rules.
			$this->form_validation->set_rules('fullname','First Name','required|regex_match[/^[A-Za-z\. ]+$/]');
			$this->form_validation->set_rules('email','Email','required|trim|valid_email|xss_clean');
			$this->form_validation->set_rules('password','Password','required|trim');
			$this->form_validation->set_rules('confirm_password','Confirm Password','required|trim|matches[password]');
			// Run the validation.
				if ($this->form_validation->run())
				{
					$this->data['post_value'] = $this->input->post();
					if($this->admin_model->checkPanelist($this->data['post_value']['email']))
					{
						$this->data['error']='Email already exists';
						$this->load->view('admin/manage_admin/create_panelist',$this->data);
						
					}
					else
					{
						$val = $this->admin_model->addPanelist($this->data['post_value']);
						if($val > 0)
						{
							$this->session->set_userdata('panelist_success', 'Panelist added successfully');
							header('Location: '.BASE_URL.'/'.$admin.'/create_panelist');
							redirect(BASE_URL.'/'.$this->admin.'/create_panelist');
							exit();
						}
						else
						{
							$this->data['error']='Problem in creating Event';
							$this->load->view('admin/manage_admin/create_panelist',$this->data);
						}
					}
				}
				else
				{
					$this->data['post_value'] = $this->input->post();	
					$this->data['error'] = validation_errors();
					$this->load->view('admin/manage_admin/create_panelist',$this->data);
				}
				$this->load->view('admin/manage_admin/create_panelist',$this->data);
			}
			else
			{
				$this->load->view('admin/manage_admin/create_panelist',$this->data);	
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	
	
	public function managePanelist($value='')
	{
		if($this->session->userdata('admin_id') && $this->session->userdata('admin_id') != '' && $this->session->userdata('logedin'))
		{
			$search_data = 'All';
			if($this->input->post('panelist_search_name'))
			{
				// Set validation rules.
				$this->form_validation->set_rules('panelist_search_name', 'Panelist Search Name','trim|required|xss_clean');
				// Run the validation.
				if ($this->form_validation->run())
				{
					$this->data['post_value'] = $this->input->post();
					$search_data = $this->data['post_value']['panelist_search_name'];
					$this->data['search_tag'] = $search_data;
					$this->data['search_value'] = $this->admin_model->getPanelistByName($search_data, '0', '10');
					$this->data['search_count'] = $this->admin_model->getPanelistByName($search_data, '', '');
					$this->load->view('admin/manage_admin/manage_panelist',$this->data);
				}
				else
				{
					$this->data['post_value'] = $this->input->post();
					$this->data['error'] = validation_errors();
					$this->load->view('admin/manage_admin/manage_panelist',$this->data);
				}
			}
			else if($this->input->post('affiliation2'))
			{
				// Set validation rules.
				$this->form_validation->set_rules('affiliation2', 'Affiliation','trim|required|xss_clean');
				// Run the validation.
				if ($this->form_validation->run())
				{
					$this->data['post_value'] = $this->input->post();
					$search_data = $this->data['post_value']['affiliation2'];
					$this->data['search_tag1'] = $search_data;
					$this->data['search_value'] = $this->admin_model->getPanelistByAff($search_data, '0', '10');
					$this->data['search_count'] = $this->admin_model->getPanelistByAff($search_data, '', '');
					$this->load->view('admin/manage_admin/manage_panelist',$this->data);
				}
				else
				{
					$this->data['post_value'] = $this->input->post();
					$this->data['error'] = validation_errors();
					$this->load->view('admin/manage_admin/manage_panelist',$this->data);
				}
			}
			else if($this->input->get('page'))
			{
				$this->data['search_count'] = $this->admin_model->getPanelistByName($search_data, '', '');
				$page = $this->input->get('page');
				if($page <= 1)
				{
					$start = 0;
				}else{
					$start = ($page-1)*10;
				}
				$this->data['search_value'] = $this->admin_model->getPanelistByName($search_data, $start, '10');
				$this->load->view('admin/manage_admin/manage_panelist',$this->data);
			}
			else
			{
				$this->data['search_count'] = $this->admin_model->getPanelistByName($search_data, '', '');
				$this->data['search_value'] = $this->admin_model->getPanelistByName($search_data, '0', '10');
				$this->load->view('admin/manage_admin/manage_panelist',$this->data);	
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	public function modifyPanelist($value='')
	{
		if($this->session->userdata('admin_id') && $this->session->userdata('admin_id') != '' && $this->session->userdata('logedin'))
		{
			$search_data = 'All';
			
			
			if($this->input->get('id') && $this->input->get('id') != '' && preg_match('/^[0-9]+$/',$this->input->get('id')))
			{
				if($this->input->get('type') && $this->input->get('type') != '' && preg_match("/^[A-Za-z]+$/",$this->input->get('type')))
				{
					$this->data['get_value'] = $this->input->get();
					if($this->input->get('type') == 'delete')
					{
						$this->admin_model->updatePanelistStatus($this->input->get('id'), 'Deleted');
						$this->session->set_userdata('panelist_success', 'Panelist deleted successfully');
						header('Location: '.BASE_URL.'/'.$this->admin.'/manage_panelist');
						redirect(BASE_URL.'/'.$this->admin.'/manage_panelist');
						exit();
					}
					if($this->input->get('type') == 'Active' || $this->input->get('type') == 'Inactive')
					{
						if($this->input->get('type') == 'Active') $stat = 'Inactive';
						if($this->input->get('type') == 'Inactive') $stat = 'Active';
						$this->admin_model->updatePanelistStatus($this->input->get('id'), $stat);
						$this->session->set_userdata('panelist_success', 'Panelist is now '.$stat);
						header('Location: '.BASE_URL.'/'.$this->admin.'/manage_panelist');
						redirect(BASE_URL.'/'.$this->admin.'/manage_panelist');
						exit();
					}
					if($this->input->get('type') == 'edit')
					{
						$this->session->set_userdata('edit_panelist', $this->input->get('id'));
						header('Location: '.BASE_URL.'/'.$this->admin.'/edit_panelist');
						redirect(BASE_URL.'/'.$this->admin.'/edit_panelist');
						exit();
					}
				}
				else {
					header('Location: '.BASE_URL.'/'.$this->admin.'/manage_panelist');
					redirect(BASE_URL.'/'.$this->admin.'/manage_panelist');
					exit();
				}
			}
			else
			{
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_admin');
				redirect(BASE_URL.'/'.$this->admin.'/manage_admin');
				exit();
			}
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	public function editPanelist($value='')
	{

		if($this->session->userdata('admin_id') && $this->session->userdata('admin_id') != '' && $this->session->userdata('logedin'))
		{
			if($this->session->userdata('edit_panelist'))
			{
				$this->data['post_value'] = $this->admin_model->getPanelistById($this->session->userdata('edit_panelist'));
			}	
			else
			{
				header('Location: '.BASE_URL.'/'.$this->admin.'/manage_panelist');
				redirect(BASE_URL.'/'.$this->admin.'/manage_panelist');
				exit();
			}
			if($this->input->post('fullname'))
			{
				// Set validation rules.
				$this->form_validation->set_rules('fullname','First Name','required|regex_match[/^[A-Za-z\. ]+$/]');
				$this->form_validation->set_rules('email','Email','required|trim|valid_email|xss_clean');
				$this->form_validation->set_rules('password','Password','trim');
				$this->form_validation->set_rules('confirm_password','Confirm Password','trim|matches[password]');
			
				// Run the validation.
				if ($this->form_validation->run())
				{
					$this->data['post_value'] = $this->input->post();
					$val = $this->admin_model->updatePanelist($this->data['post_value']);
					$this->session->set_userdata('panelist_success', 'Panelist updated succefully');
					$this->session->set_userdata('edit_panelist', '');
					header('Location: '.BASE_URL.'/'.$this->admin.'/edit_panelist');
					redirect(BASE_URL.'/'.$this->admin->admin.'/edit_panelist');
					exit();
				}
				else
				{
					$this->data['error'] = validation_errors();
				//	$this->data['post_value'] = $this->admin_model->getPanelistById($this->session->userdata('edit_panelist'));
				//	$this->load->view('admin/manage_admin/edit_panelist',$this->data);
				}
			}
			$this->load->view('admin/manage_admin/edit_panelist',$this->data);
		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	
	public function updateProfile($value="")
	{
		if($this->session->userdata('admin_id') && $this->session->userdata('admin_id') != '' && $this->session->userdata('logedin'))
		{
			
			if($this->session->userdata('admin_id'))
			{
				$this->data['post_value'] = $this->admin_model->getPanelistById($this->session->userdata('admin_id'));
			}	
			else
			{
				header('Location: '.BASE_URL.'/'.$this->admin.'/dashboard');
				redirect(BASE_URL.'/'.$this->admin.'/dashboard');
				exit();
			}
			if($this->input->post('fullname'))
			{
				// Set validation rules.
				$this->load->library('form_validation');
				$this->form_validation->set_rules('fullname','First Name','required|regex_match[/^[A-Za-z\. ]+$/]');
				$this->form_validation->set_rules('email','Email','required|trim|valid_email|xss_clean');
				$this->form_validation->set_rules('password','Password','trim');
				$this->form_validation->set_rules('confirm_password','Confirm Password','trim|matches[password]');
				// Run the validation.
				if ($this->form_validation->run())
				{
					if($this->admin_model->updatePanelist_profile()){
						$this->session->set_userdata('panelist_success', 'Profile updated succefully');
					}
					header('Location: '.BASE_URL.'/'.$admin.'/update_profile');
					redirect(BASE_URL.'/'.$this->admin.'/update_profile');
					exit();
				}
				else
				{
					$this->load->view('admin/manage_admin/update_profile',$this->data);
				}
				
			}
			else {	
					$this->load->view('admin/manage_admin/update_profile',$this->data);
			}

		}
		else{
			$this->session->sess_destroy();
			header('Location: '.BASE_URL.'/login');
			redirect(BASE_URL.'/login');
			exit();
		}
	}
	
	function manageApplication($value="")
	{
		$data = array();
		$data['result'] = $this->admin_model->getAwardapplication_panalist();
		for($i= 0; $i < count($data['result']); $i++){
			$id = $data['result'][$i]->award_panelist_award_id;
			if($id){
				$data['result1'][$i] = $this->admin_model->award_name($id);
			}
			if($data['result1'][$i]){
				$data['result2'][$i] = $this->admin_model->get_inchargename($data['result1'][$i][0]->award_incharge_id);
			}
			if($id) {
				$data['result3'][$i] = $this->admin_model->get_chief_panelist($id);
			}
		}
	$this->load->view('admin/manage_admin/manage_application',$data);
	}
	
	public function panelist_awards_view(){
		$award_id = $this->input->get('award_id');
		$data['result'] = $this->admin_model->award_name($award_id);
		if($data['result']){
			$data['result1'] = $this->admin_model->get_inchargename($data['result'][0]->award_incharge_id);
		}
		$data['result2'] = $this->admin_model->get_all_panelist($award_id);
		if($data['result2']) {
			for($i = 0; $i < count($data['result2']); $i++){
				$data['result3'][$i] = $this->admin_model->get_inchargename($data['result2'][$i]->award_panelist_admin_id); 
			}
		}
		$this->load->view('admin/manage_admin/panelist_view_award',$data);
	}
}
?>